package br.boicotando.dao;

import org.hibernate.ObjectNotFoundException;
import org.hibernate.Session;

import br.boicotando.server.Bau;

public abstract class DaoDB<E> implements Dao<E> {
	
	protected Session sess;

	public static Session abreSessao() {
		return Bau.getFabricaSessao().openSession();
	}

	public DaoDB(Session sess) {
		this.sess = sess;
	}

	@Override
	public void salvar(E obj) {
		try {
			sess.merge(obj);
			sess.update(obj);
		} catch (ObjectNotFoundException e) {
			sess.persist(obj);
		}
	}

	@Override
	public void deletar(E obj) {
		sess.delete(obj);
	}
	
}
